package com.infindo.appcreate.thread.queue;

import java.lang.reflect.Field;

import com.infindo.appcreate.zzyj.util.StringUtil;
import com.infindo.appcreate.zzyj.util.ThreadGroupEnum;

public class  TaskThread<T> extends Thread {

	private final ThreadCallback callback;
	private T myTask; 
	
	public TaskThread(ThreadCallback callback, T myTask) {
		this.callback = callback;
		this.myTask = myTask;
	}
	
	public synchronized void run() {
		try {
			
			Field fieldGroupName = myTask.getClass().getField("groupName");
			String groupName = (String)fieldGroupName.get(myTask);
			
			if(StringUtil.isNotBlank(groupName) && groupName.equals(ThreadGroupEnum.build.toString())){
				myTask.getClass().getMethod("taskRun", null).invoke(myTask, null);
			}else if(StringUtil.isBlank(groupName)){
				myTask.getClass().getMethod("executeTask", null).invoke(myTask, null);
			}
			System.out.println("======TaskThread " + getId() + " End");
		} catch (Exception e) {
		    e.printStackTrace();
		} finally {
		    callback.notifyThreadEnd(getId());
		}
	}

}
